package com.smartappsnetwork.personalexpenses.dataaccess;

import java.util.Vector;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

import com.smartappsnetwork.personalexpenses.LauncherApplication;
import com.smartappsnetwork.personalexpenses.database.DatabaseHelper;
import com.smartappsnetwork.personalexpenses.dataobject.PC_GeneralSettingsDO;

public class PC_GeneralSettingsDA 
{

	public long insertGeneralSettings(PC_GeneralSettingsDO generalSettingsDO)
	{
		synchronized (LauncherApplication.databaseLock) 
		{ 
			 long appID = -1;
			 SQLiteDatabase database =null;
			 
			 String query = "SELECT * FROM tblParentalCntrolGeneralSettings WHERE psid=?";
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
					 //1- true , 0 -false
					 Cursor cursor = database.rawQuery(query, new String[]{""+generalSettingsDO.psId});
					 if(cursor!=null && cursor.moveToNext())
					 {
						 updateGeneralSettings(generalSettingsDO);
					 }
					 else
					 {
						 SQLiteStatement stmt    = database.compileStatement("INSERT INTO tblParentalCntrolGeneralSettings (psId, timeLimitPerDay, timeLimitPerWeek, timeReminder, isTimeLimitPerDay, isTimeLimitPerWeek, isTimeLimitPerApp, timeLimitPerDayTemp, timeLimitPerWeekTemp) VALUES (?,?,?,?,?,?,?,?,?)");
						 stmt.bindLong(1, generalSettingsDO.psId);
						 stmt.bindLong(2,generalSettingsDO.timeLimitPerDay);
						 stmt.bindLong(3, generalSettingsDO.timeLimitPerWeek);
						 stmt.bindLong(4,generalSettingsDO.timeReminder);
						 stmt.bindLong(5,generalSettingsDO.isTimeLimitPerDay?1:0);
						 stmt.bindLong(6,generalSettingsDO.isTimeLimitPerWeek?1:0);
						 stmt.bindLong(7,generalSettingsDO.isTimeLimitPerApp?1:0);
						 stmt.bindLong(8, generalSettingsDO.timeLimitPerDay);
						 stmt.bindLong(9, generalSettingsDO.timeLimitPerWeek);
						 appID = stmt.executeInsert();
						 stmt.close();
					 }
				 }
			 }
			 catch(Exception e)
			 {
				 Log.e("tblParentalCntrolGeneralSettings", ""+e);
				 e.printStackTrace();
			 }
			 finally
			 {
				 if(database!=null && database.isOpen())
					 database.close();
			 }
				
			return appID;
		}
	}
	
	
	
	public long  updateGeneralSettings(PC_GeneralSettingsDO generalSettingsDO)
	{
		synchronized (LauncherApplication.databaseLock) 
		{ 
			 long appID = -1;
			 SQLiteDatabase database =null;
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
						
					 //1- true , 0 -false
					 SQLiteStatement stmt = database.compileStatement(
							 "UPDATE tblParentalCntrolGeneralSettings SET  " +
							 "timeLimitPerDay ='"+ (generalSettingsDO.timeLimitPerDay)+
							 "',timeLimitPerWeek = '"+ (generalSettingsDO.timeLimitPerWeek)
							 +"', timeReminder = '"+ (generalSettingsDO.timeReminder)+
							 "', isTimeLimitPerDay = '"+ (generalSettingsDO.isTimeLimitPerDay?1:0)+
							 "', isTimeLimitPerWeek = '"+(generalSettingsDO.isTimeLimitPerWeek?1:0)+
							 "', isTimeLimitPerApp = '"+  (generalSettingsDO.isTimeLimitPerApp?1:0) +
							 "', timeLimitPerDayTemp ='"+ (generalSettingsDO.timeLimitPerDay)+
							 "',timeLimitPerWeekTemp = '"+ (generalSettingsDO.timeLimitPerWeek)+"' where psId='"+generalSettingsDO.psId+"'");
					 
					 appID = stmt.executeInsert();
					 stmt.close();
				 }
			 }
			 catch(Exception e)
			 {
				 Log.e("update", "tblParentalCntrolGeneralSettings : "+e);
				 e.printStackTrace();
			 }
			 finally
			 {
				 if(database!=null && database.isOpen())
					 database.close();
			 }
				
			return appID;
		}
	}
	
	
	public long  update_timeReminder(long timeReminder, long psid)
	{
		synchronized (LauncherApplication.databaseLock) 
		{ 
			 long appID = -1;
			 SQLiteDatabase database =null;
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
						
					 //1- true , 0 -false
					 SQLiteStatement stmt = database.compileStatement(
							 "UPDATE tblParentalCntrolGeneralSettings SET  " +
							 "', timeReminder = '"+ timeReminder+ "' where psId='"+psid+"'");
					 
					 appID = stmt.executeInsert();
					 stmt.close();
				 }
			 }
			 catch(Exception e)
			 {
				 Log.e("update", "tblParentalCntrolGeneralSettings : "+e);
				 e.printStackTrace();
			 }
			 finally
			 {
				 if(database!=null && database.isOpen())
					 database.close();
			 }
				
			return appID;
		}
	}
	
	public long  update_timeLimitPerDay(long timeLimitPerDay, long psid)
	{
		synchronized (LauncherApplication.databaseLock) 
		{ 
			 long appID = -1;
			 SQLiteDatabase database =null;
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
						
					 //1- true , 0 -false
					 SQLiteStatement stmt = database.compileStatement(
							 "UPDATE tblParentalCntrolGeneralSettings SET  " +
							 "', timeLimitPerDay = '"+ timeLimitPerDay+ "' where psId='"+psid+"'");
					 
					 appID = stmt.executeInsert();
					 stmt.close();
				 }
			 }
			 catch(Exception e)
			 {
				 Log.e("update", "tblParentalCntrolGeneralSettings : "+e);
				 e.printStackTrace();
			 }
			 finally
			 {
				 if(database!=null && database.isOpen())
					 database.close();
			 }
				
			return appID;
		}
	}
	
	public long  update_timeLimitPerWeek(long timeLimitPerWeek, long psid)
	{
		synchronized (LauncherApplication.databaseLock) 
		{ 
			 long appID = -1;
			 SQLiteDatabase database =null;
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
						
					 //1- true , 0 -false
					 SQLiteStatement stmt = database.compileStatement(
							 "UPDATE tblParentalCntrolGeneralSettings SET  " +
							 "', timeLimitPerWeek = '"+ timeLimitPerWeek+ "' where psId='"+psid+"'");
					 
					 appID = stmt.executeInsert();
					 stmt.close();
				 }
			 }
			 catch(Exception e)
			 {
				 Log.e("update", "tblParentalCntrolGeneralSettings : "+e);
				 e.printStackTrace();
			 }
			 finally
			 {
				 if(database!=null && database.isOpen())
					 database.close();
			 }
				
			return appID;
		}
	}
	
	public long  update_timeLimitPerDayTemp(long timeLimitPerDayTemp, long psid)
	{
		synchronized (LauncherApplication.databaseLock) 
		{ 
			 long appID = -1;
			 SQLiteDatabase database =null;
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
						
					 //1- true , 0 -false
					 SQLiteStatement stmt = database.compileStatement(
							 "UPDATE tblParentalCntrolGeneralSettings SET  " +
							 "', timeLimitPerDayTemp = '"+ timeLimitPerDayTemp+ "' where psId='"+psid+"'");
					 
					 appID = stmt.executeInsert();
					 stmt.close();
				 }
			 }
			 catch(Exception e)
			 {
				 Log.e("update", "tblParentalCntrolGeneralSettings : "+e);
				 e.printStackTrace();
			 }
			 finally
			 {
				 if(database!=null && database.isOpen())
					 database.close();
			 }
				
			return appID;
		}
	}
	
	public long  update_timeLimitPerWeekTemp(long timeLimitPerWeekTemp, long psid)
	{
		synchronized (LauncherApplication.databaseLock) 
		{ 
			 long appID = -1;
			 SQLiteDatabase database =null;
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
						
					 //1- true , 0 -false
					 SQLiteStatement stmt = database.compileStatement(
							 "UPDATE tblParentalCntrolGeneralSettings SET  " +
							 "', timeLimitPerWeekTemp = '"+ timeLimitPerWeekTemp+ "' where psId='"+psid+"'");
					 
					 appID = stmt.executeInsert();
					 stmt.close();
				 }
			 }
			 catch(Exception e)
			 {
				 Log.e("update", "tblParentalCntrolGeneralSettings : "+e);
				 e.printStackTrace();
			 }
			 finally
			 {
				 if(database!=null && database.isOpen())
					 database.close();
			 }
				
			return appID;
		}
	}
	
	
	public PC_GeneralSettingsDO get_GeneralSettings_by_psId(long psId)
	{
		synchronized (LauncherApplication.databaseLock) 
		{
			 SQLiteDatabase database =null;
			 Cursor cursor = null;
			 PC_GeneralSettingsDO generalSettingsDO = null;
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
					 String query  = "SELECT * FROM tblParentalCntrolGeneralSettings WHERE psId ='"+psId+"'";
					 cursor = database.rawQuery(query, null);
					 if(cursor!=null && cursor.moveToFirst())
					 {
						 do
						 {
							 generalSettingsDO = new PC_GeneralSettingsDO();
							 generalSettingsDO.gpId   	  			=  cursor.getLong(0);
							 generalSettingsDO.psId  	  			=  cursor.getLong(1);
							 generalSettingsDO.timeLimitPerDay 		=  cursor.getLong(2);
							 generalSettingsDO.timeLimitPerWeek  	=  cursor.getLong(3);
							 generalSettingsDO.timeReminder			=  cursor.getLong(4);
							 generalSettingsDO.isTimeLimitPerDay	=  cursor.getLong(5)==1?true:false;
							 generalSettingsDO.isTimeLimitPerWeek	=  cursor.getLong(6)==1?true:false;
							 generalSettingsDO.isTimeLimitPerApp	=  cursor.getLong(7)==1?true:false;
							 generalSettingsDO.timeLimitPerDayTemp	=  cursor.getLong(8);
							 generalSettingsDO.timeLimitPerWeekTemp =  cursor.getLong(9);
						 }
						 while(cursor.moveToNext());
					 }
				 }
			 }
			 catch(Exception e)
			 {
				 e.printStackTrace();
			 }
			 finally
			 {
				 if(cursor!=null && !cursor.isClosed())
					 cursor.close();
				 
				 if(database!=null && database.isOpen())
					 database.close();
			 }
			 
			 return generalSettingsDO;
		}
	}
	
	public PC_GeneralSettingsDO get_GeneralSettings_by_gpId(int gpId)
	{
		synchronized (LauncherApplication.databaseLock) 
		{
			 SQLiteDatabase database =null;
			 Cursor cursor = null;
			 PC_GeneralSettingsDO generalSettingsDO = new PC_GeneralSettingsDO();
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
					 String query  = "SELECT *FROM tblParentalCntrolGeneralSettings WHERE psId ='"+gpId+"'";
					 cursor = database.rawQuery(query, null);
					 if(cursor!=null && cursor.moveToFirst())
					 {
						 do
						 {
							 generalSettingsDO.gpId   	  			=  cursor.getLong(0);
							 generalSettingsDO.psId  	  			=  cursor.getLong(1);
							 generalSettingsDO.timeLimitPerDay 		=  cursor.getLong(2);
							 generalSettingsDO.timeLimitPerWeek  	=  cursor.getLong(3);
							 generalSettingsDO.timeReminder			=  cursor.getLong(4);
							 generalSettingsDO.isTimeLimitPerDay	=  cursor.getLong(5)==1?true:false;
							 generalSettingsDO.isTimeLimitPerWeek	=  cursor.getLong(6)==1?true:false;
							 generalSettingsDO.isTimeLimitPerApp	=  cursor.getLong(7)==1?true:false;
							 generalSettingsDO.timeLimitPerDayTemp	=  cursor.getLong(8);
							 generalSettingsDO.timeLimitPerWeekTemp =  cursor.getLong(9);
						 }
						 while(cursor.moveToNext());
					 }
				 }
			 }
			 catch(Exception e)
			 {
				 e.printStackTrace();
			 }
			 finally
			 {
				 if(cursor!=null && !cursor.isClosed())
					 cursor.close();
				 
				 if(database!=null && database.isOpen())
					 database.close();
			 }
			 
			 return generalSettingsDO;
		}
	}
	
	public Vector<PC_GeneralSettingsDO> get_All_GeneralSettings()
	{
		synchronized (LauncherApplication.databaseLock) 
		{
			 SQLiteDatabase database =null;
			 Cursor cursor = null;
			 Vector<PC_GeneralSettingsDO> vecGeneralSettingsDO = new Vector<PC_GeneralSettingsDO>();
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
					 String query  = "SELECT *FROM tblParentalCntrolGeneralSettings";
					 cursor = database.rawQuery(query, null);
					 if(cursor!=null && cursor.moveToFirst())
					 {
						 do
						 {
							 PC_GeneralSettingsDO generalSettingsDO = new PC_GeneralSettingsDO();
							 generalSettingsDO.gpId   	  			=  cursor.getLong(0);
							 generalSettingsDO.psId  	  			=  cursor.getLong(1);
							 generalSettingsDO.timeLimitPerDay 		=  cursor.getLong(2);
							 generalSettingsDO.timeLimitPerWeek  	=  cursor.getLong(3);
							 generalSettingsDO.timeReminder			=  cursor.getLong(4);
							 generalSettingsDO.isTimeLimitPerDay	=  cursor.getLong(5)==1?true:false;
							 generalSettingsDO.isTimeLimitPerWeek	=  cursor.getLong(6)==1?true:false;
							 generalSettingsDO.isTimeLimitPerApp	=  cursor.getLong(7)==1?true:false;
							 generalSettingsDO.timeLimitPerDayTemp	=  cursor.getLong(8);
							 generalSettingsDO.timeLimitPerWeekTemp =  cursor.getLong(9);
							 vecGeneralSettingsDO.add(generalSettingsDO);
						 }
						 while(cursor.moveToNext());
					 }
				 }
			 }
			 catch(Exception e)
			 {
				 e.printStackTrace();
			 }
			 finally
			 {
				 if(cursor!=null && !cursor.isClosed())
					 cursor.close();
				 
				 if(database!=null && database.isOpen())
					 database.close();
			 }
			 
			 return vecGeneralSettingsDO;
		}
	}
	

	public void delete_GeneralSettings_By_psId(int psId)
	{
		synchronized (LauncherApplication.databaseLock) 
		{
			 SQLiteDatabase database =null;
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
	
					 String whereClause = "psId=?";
					 database.delete("tblParentalCntrolGeneralSettings", whereClause, new String[]{""+psId});
				 }
			 }
			 catch(Exception e)
			 {
				 e.printStackTrace();
			 }
			 finally
			 {
				 
				 if(database!=null && database.isOpen())
					 database.close();
			 }
		}
		 
	}
	
	public void delete_GeneralSettings_By_gpId(int gpId)
	{
		synchronized (LauncherApplication.databaseLock) 
		{
			 SQLiteDatabase database =null;
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
	
					 String whereClause = "gpId=?";
					 database.delete("tblParentalCntrolGeneralSettings", whereClause, new String[]{""+gpId});
				 }
			 }
			 catch(Exception e)
			 {
				 e.printStackTrace();
			 }
			 finally
			 {
				 
				 if(database!=null && database.isOpen())
					 database.close();
			 }
		}
		 
	}
	
	public void delete_ALL_GeneralSettings()
	{
		synchronized (LauncherApplication.databaseLock) 
		{
			 SQLiteDatabase database =null;
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
					 database.delete("tblParentalCntrolGeneralSettings", null, null);
				 }
			 }
			 catch(Exception e)
			 {
				 e.printStackTrace();
			 }
			 finally
			 {
				 
				 if(database!=null && database.isOpen())
					 database.close();
			 }
		}
	}
	
	
	public static void resetDayTempValues()
	{
		synchronized (LauncherApplication.databaseLock) 
		{
			 SQLiteDatabase database =null;
			 try
			 {
				 database      =  DatabaseHelper.openDataBase();
				 String query  =  "update tblParentalCntrolGeneralSettings set timeLimitPerDayTemp = timeLimitPerDay";
				 SQLiteStatement sqLiteStatement = database.compileStatement(query);
				 sqLiteStatement.executeInsert();
				 sqLiteStatement.close();
			 }
			 catch(Exception e)
			 {
				 e.printStackTrace();
			 }
			 finally
			 {
				 
				 if(database!=null && database.isOpen())
					 database.close();
			 }
		}
	}
	
	public static void resetWeekTempValues()
	{
		synchronized (LauncherApplication.databaseLock) 
		{
			 SQLiteDatabase database =null;
			 try
			 {
				 database      =  DatabaseHelper.openDataBase();
				 String query  =  "update tblParentalCntrolGeneralSettings set timeLimitPerWeekTemp = timeLimitPerWeek";
				 SQLiteStatement sqLiteStatement = database.compileStatement(query);
				 sqLiteStatement.executeInsert();
				 sqLiteStatement.close();
			 }
			 catch(Exception e)
			 {
				 e.printStackTrace();
			 }
			 finally
			 {
				 
				 if(database!=null && database.isOpen())
					 database.close();
			 }
		}
	}


}
